package com.tudouji.project.handover.service.impl;

import com.google.gson.annotations.Until;
import com.tudouji.common.utils.IdUtils;
import com.tudouji.common.utils.SecurityUtils;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.project.customer.domain.Customer;
import com.tudouji.project.customer.domain.Customerdepartgroup;
import com.tudouji.project.customer.mapper.CustomerMapper;
import com.tudouji.project.customer.mapper.CustomerdepartgroupMapper;
import com.tudouji.project.handover.domain.CustomerVo;
import com.tudouji.project.handover.mapper.CustomerVoMapper;
import com.tudouji.project.handover.service.CustomerVoService;
import com.tudouji.project.system.domain.SysUser;
import com.tudouji.project.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CustomerVoServiceImpl implements CustomerVoService {

    @Autowired
    private CustomerVoMapper customerVoMapper;
//    部门客户mapper接口
    @Autowired
    private CustomerdepartgroupMapper customerdepartgroupMapper;
//  系统用户mapper接口
    @Autowired
    private SysUserMapper sysUserMapper;
//    客户表mapper接口
    @Autowired
    private CustomerMapper customerMapper;

    /**
     * 查询客户列表信息
     * @param customerVo
     * @return
     */
    @Override
    public List<CustomerVo> selectCustomerVoList(CustomerVo customerVo) {
        return customerVoMapper.selectCustomerVoList(customerVo);
    }

    /**
     * 根据guid查询客户详细信息
     * @param guid
     * @return
     */
    @Override
    public List<CustomerVo> selectCustomerVoByGuid(String guid) {
        return customerVoMapper.selectCustomerVoByGuid(guid);
    }

    /**
     * 根据用户id查询客户信息
     * @param
     * @return
     */
    @Override
    public List<CustomerVo> selectCustomerVoByUserid(Long userId,String customerid){
        return customerVoMapper.selectCustomerVoByUserid(userId,customerid);
    }


    /**
     * 移交客户信息
     * @param
     * @return
     */
    @Override
    @Transactional
    public AjaxResult updateCustomerVo(Long fromUserId,Long toUserId,String[] guid,String picked) {
//        创建人
        SysUser user= SecurityUtils.getLoginUser().getUser();
//        交出人
        SysUser fromUser=sysUserMapper.selectUserById(fromUserId);
//        接受人
        SysUser toUser=sysUserMapper.selectUserById(toUserId);


        List<CustomerVo> customerVoList=new ArrayList<>();

        if (guid.length>=1){
            //        根据选中的guid查询客户信息
            for (String s : guid) {
                List<CustomerVo> customerVoList1 = customerVoMapper.selectCustomerVoByGuid(s);
                customerVoList.addAll(customerVoList1);
            }

            if (picked.equals("1")){
                for (CustomerVo customerVo : customerVoList) {
//                    查询接收人是否存在选中的客户
                    List<CustomerVo> customerVoList2 = customerVoMapper.selectCustomerVoByUserid(toUserId, customerVo.getCustomerid());
                    if (customerVoList2.isEmpty()) {
                        //        更新部门用户关系
                        customerVo.setGuid(IdUtils.fastSimpleUUID());
                        customerVo.setSellerid(toUser.getGuid());
                        customerVoMapper.insertCustomerVo(customerVo);

                    } else {
//                    return AjaxResult.error("移交客户已存在");
                        System.out.println("移交客户已存在");
                    }

                }

            }else {
                for (CustomerVo customerVo : customerVoList) {
                    List<CustomerVo> customerVoList2 = customerVoMapper.selectCustomerVoByUserid(toUserId, customerVo.getCustomerid());
                    if (customerVoList2.isEmpty()) {
                        //        更新部门用户关系
                        customerVo.setSellerid(toUser.getGuid());
                        customerVoMapper.updateCustomerVoSeller(customerVo);

                    } else {
//                    return AjaxResult.error("移交客户已存在");
                        System.out.println("移交客户已存在");
                    }
                }
            }

        }else {
            return AjaxResult.error("请选择需移交的客户！");
        }

        return AjaxResult.success();
    }

    /**
     * 修改部门用户关系
     * @param customerVo
     * @return
     */
    @Override
    public int updateCustomerVoSeller(CustomerVo customerVo) {
        return customerVoMapper.updateCustomerVoSeller(customerVo);
    }

    /**
     * 新增客户经理关系
     * @param customerVo
     * @return
     */
    @Override
    public int insertCustomerVo(CustomerVo customerVo){
        return customerVoMapper.insertCustomerVo(customerVo);
    }

}
